package com.ybkj.daijia.server.event.listener.zhuan;

import com.ybkj.daijia.PushData;
import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.api.gexin.GeTuiMember;
import com.ybkj.daijia.server.event.model.zhuan.ZhaunOrderArriveEvent;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.zhuan.Order;
import java.io.IOException;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class ZhaunOrderArriveEventListener implements ApplicationListener<ZhaunOrderArriveEvent> {

    private static final Logger logger = LoggerFactory
        .getLogger(ZhaunOrderArriveEventListener.class);


    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;

    @Override
    @Async
    public void onApplicationEvent(ZhaunOrderArriveEvent event) {

        Order order = (Order) event.getSource();
        Passenger passenger = event.getPassenger();
        passenger.setCid(memberService.findCid(passenger.getId()));
        if (StringUtils.isNotBlank(passenger.getCid())) {
            try {
                //发送设备推送
                String address = "";
                if (order != null) {
                    address = order.getStartAddress();
                }
                PushData pushData = new PushData();
                pushData.setType("WAIT_ORDER_ZHUANCHE");
                pushData.setTitle("服务人员到达预约地");
                pushData.setDescription("服务人员已到达预约地【" + address + "】");
                pushData.setData(order.getId());
                GeTuiMember.push(passenger, pushData, settingUtils.get());
            } catch (IOException e) {
                e.getMessage();
            }

        }

    }

}
